{ INFORMATICS.MCCME.RU
  Problem statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=28
  Verdict: Accepted
}
type date=record
     d,m,y:integer;
end;
type person=record
     b,d:date; n:longint;
end;
var i,n,cur,j,g,t:longint; k:date; b,e,h,next,prev:array[1..10001] of longint; f,fl:boolean; a:array[1..10001] of person;
function com(a,b:date):boolean;
begin
if a.y>b.y then com:=true else
if a.y<b.y then com:=false else begin
   if a.m>b.m then com:=true else
   if a.m<b.m then com:=false else begin
      if a.d>b.d then com:=true else
      if a.d<=b.d then com:=false;
   end;
end;
end;
procedure qsort_2(l,r:longint);
var i,j,x,y:longint;
begin
i:=l;
j:=r;
x:=h[(l+r) div 2];
repeat
      while h[i]<x do inc(i);
      while h[j]>x do dec(j);
      if i<=j then begin
         y:=h[i];
         h[i]:=h[j];
         h[j]:=y;
         inc(i);
         dec(j);
      end;
until i>j;
if l<j then qsort_2(l,j);
if i<r then qsort_2(i,r);
end;
procedure qsort_1(l,r:longint);
var i,j,t:longint; x:date; y:person;
begin
i:=l;
j:=r;
x:=a[b[(l+r) div 2]].b;
repeat
      while com(x,a[b[i]].b) do inc(i);
      while com(a[b[j]].b,x) do dec(j);
      if i<=j then begin
         t:=b[i];
         b[i]:=b[j];
         b[j]:=t;
         inc(i);
         dec(j);
      end;
until i>j;
if l<j then qsort_1(l,j);
if i<r then qsort_1(i,r);
end;
procedure qsort_3(l,r:longint);
var i,j,t:longint; x:date; y:person;
begin
i:=l;
j:=r;
x:=a[e[(l+r) div 2]].d;
repeat
      while com(x,a[e[i]].d) do inc(i);
      while com(a[e[j]].d,x) do dec(j);
      if i<=j then begin
         t:=e[i];
         e[i]:=e[j];
         e[j]:=t;
         inc(i);
         dec(j);
      end;
until i>j;
if l<j then qsort_3(l,j);
if i<r then qsort_3(i,r);
end;
begin
assign(input,'input.txt');
assign(output,'output.txt');
reset(input);
rewrite(output);
readln(n);
cur:=0;
for i:=1 to n do begin
    inc(cur);
    read(a[cur].b.d,a[cur].b.m,a[cur].b.y);
    k:=a[cur].b;
    inc(a[cur].b.y,18);
    read(a[cur].d.d,a[cur].d.m,a[cur].d.y);
    if not com(a[cur].b,a[cur].d) then begin
       inc(k.y,80);
       if com(a[cur].d,k) then a[cur].d:=k;
       b[cur]:=cur;
       e[cur]:=cur;
       a[cur].n:=i;
    end else dec(cur);
end;
qsort_1(1,cur);
qsort_3(1,cur);
for i:=2 to cur do begin
    next[b[i-1]]:=b[i];
    prev[b[i]]:=b[i-1];
end;
i:=0;
j:=1;
f:=true;
fl:=false;
while (i<cur) do begin
      inc(i);
      if ((not com(a[e[j]].d,a[b[i]].b)) and (f)) then begin
         t:=1;
         h[1]:=a[b[i-1]].n;
         g:=prev[b[i-1]];
         while g<>0 do begin
               inc(t);
               h[t]:=a[g].n;
               g:=prev[g];
         end;
         qsort_2(1,t);
         for g:=1 to t do write(h[g],' ');
         writeln;
         while ((not com(a[e[j]].d,a[b[i]].b))) do begin
               if prev[e[j]]<>0 then next[prev[e[j]]]:=next[e[j]];
               if next[e[j]]<>0 then prev[next[e[j]]]:=prev[e[j]];
               inc(j);
         end;
         //f:=false;
         fl:=true;
      end else f:=true;
end;
if (j<=cur) then begin
         t:=1;
         h[1]:=a[b[cur]].n;
         g:=prev[b[cur]];
         while g<>0 do begin
               inc(t);
               h[t]:=a[g].n;
               g:=prev[g];
         end;
         qsort_2(1,t);
         for g:=1 to t do write(h[g],' ');
         writeln;
         if prev[e[j]]<>0 then next[prev[e[j]]]:=next[e[j]];
         if next[e[j]]<>0 then prev[next[e[j]]]:=prev[e[j]];
         inc(j);
         f:=false;
         fl:=true;
      end;
if not fl then writeln(0);
end.